package com.appspot.simplebookmarks.model;

import java.io.Serializable;
import java.util.Date;

import javax.jdo.annotations.Persistent;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

import org.slim3.datastore.Attribute;
import org.slim3.datastore.Model;

@Model(schemaVersion = 1)
public class LastAccessCollection implements Serializable {

    private static final long serialVersionUID = 1L;

    @Attribute(primaryKey = true)
    private Key key;

    @Attribute(version = true)
    private Long version;

    @Persistent
    private String author;

    @Persistent
    private Key collectionKey;

    @Persistent
    private Date createDate = new Date();

    @Persistent
    private Date updateDate = new Date();

    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer("[");
        sb.append(KeyFactory.keyToString((Key) key)).append(",");
        sb.append(author).append(",");
        if (collectionKey != null) {
            sb.append(KeyFactory.keyToString((Key) collectionKey)).append(",");
        } else {
            sb.append(",");
        }
        sb.append(createDate).append(",");
        sb.append(updateDate).append(",");
        sb.append(version).append("]");
        return sb.toString();
    }

    
    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public Key getCollectionKey() {
        return collectionKey;
    }

    public void setCollectionKey(Key collectionKey) {
        this.collectionKey = collectionKey;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public Date getUpdateDate() {
        return updateDate;
    }

    public void setUpdateDate(Date updateDate) {
        this.updateDate = updateDate;
    }

    /**
     * Returns the key.
     *
     * @return the key
     */
    public Key getKey() {
        return key;
    }

    /**
     * Sets the key.
     *
     * @param key
     *            the key
     */
    public void setKey(Key key) {
        this.key = key;
    }

    /**
     * Returns the version.
     *
     * @return the version
     */
    public Long getVersion() {
        return version;
    }

    /**
     * Sets the version.
     *
     * @param version
     *            the version
     */
    public void setVersion(Long version) {
        this.version = version;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((key == null) ? 0 : key.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        LastAccessCollection other = (LastAccessCollection) obj;
        if (key == null) {
            if (other.key != null) {
                return false;
            }
        } else if (!key.equals(other.key)) {
            return false;
        }
        return true;
    }
}
